<?php

namespace App\Task\Stat;

use App\Model\Stat\StatOnlineLog;
use App\Model\UserProfile;
use Hyperf\Crontab\Annotation\Crontab;

/**
 * 在线用户统计
 * @Crontab(name="StatOnlineTask", rule="13,26,39,52 *\/1 * * *", callback="execute", memo="每日上线数据统计", singleton=false)
 */
class StatOnlineTask
{
    public function execute()
    {
        log_info(__CLASS__ . "==Start==" . date('Y-m-d H:i:s'));
        $model = new StatOnlineLog();
        $model->stat_ts = time();
        $model->online_num = UserProfile::where(['online' => 1])->count();
        $model->online_user_num = UserProfile::where(['online' => 1, 'role' => UserProfile::ROLE_USER])->count();
        $model->online_host_num = UserProfile::where(['online' => 1, 'role' => UserProfile::ROLE_HOST])->count();
        $model->online_male_num = UserProfile::where(['online' => 1, 'gender' => 1])->count();
        $model->online_female_num = UserProfile::where(['online' => 1, 'gender' => 0])->count();
        $model->saveOrFail();
    }
}
